package com.iiapk.contacts;

import android.app.ListActivity;
import android.content.AsyncQueryHandler;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.iiapk.contacts.R;
import com.iiapk.contacts.ui.widget.BladeView;
import com.iiapk.contacts.ui.widget.ContactItemListAdapter;
import com.iiapk.contacts.ui.widget.PinnedHeaderListView;

public class FullContactsActivity extends ListActivity implements View.OnClickListener,
		View.OnCreateContextMenuListener{
	
	private static final int MENU_ITEM_VIEW_CONTACT = 1;    
	private static final int MENU_ITEM_EDIT = 2;
	private static final int MENU_ITEM_DELETE = 3;
	
	private Context mContext;
	private TextView emptyText;
    private ContactItemListAdapter mAdapter;
    private QueryHandler mQueryHandler;
    private BladeView mBladeView;
    private int mCurContactId;
    
    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
    	ContactsContract.Contacts._ID,                       // 0
    	ContactsContract.Contacts.PHOTO_ID,                  // 1
    	ContactsContract.Contacts.DISPLAY_NAME,      		 // 2
    	ContactsContract.Contacts.HAS_PHONE_NUMBER,          // 3
    	ContactsContract.Contacts.SORT_KEY_PRIMARY,          // 4
    };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_list_content_ex);
        emptyText = (TextView)findViewById(R.id.emptyText);
        mContext=this;
        mQueryHandler = new QueryHandler(mContext); 
        
        mAdapter = new ContactItemListAdapter(this);        
        setListAdapter(mAdapter);
        getListView().setOnScrollListener(mAdapter);
        
        if (getListView() instanceof PinnedHeaderListView){
                PinnedHeaderListView pinnedHeaderList = (PinnedHeaderListView)getListView();
                View pinnedHeader = getLayoutInflater().inflate(R.layout.list_section_full, getListView(), false);
                pinnedHeaderList.setPinnedHeaderView(pinnedHeader);
        }
        
        mBladeView = (BladeView)this.findViewById(R.id.category);
        if (mBladeView != null) {
            mBladeView.setEnableSectionColor(Color.BLACK);
            mBladeView.setDisableSectionColor(Color.argb(0xFF, 0x6C, 0x6C, 0x6C));
        }
    }
    
    @Override  
    protected void onResume() {  
        super.onResume();  
        mQueryHandler.startQuery(0, null, ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION,
        		null, null, "sort_key COLLATE LOCALIZED asc");  
    }  
    
    @Override
	public void onClick(View v) {
        if ((v instanceof TextView) && (v.getTag()!=null)){
        	int contactId = (Integer)v.getTag();
            Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
            Intent viewContactIntent = new Intent(Intent.ACTION_VIEW, contactUri);
            startActivity(viewContactIntent);
        }
	}
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
    	 if ((view instanceof TextView) && (view.getTag()!=null)){
    		 menu.setHeaderTitle(((TextView)view).getText());
    		 mCurContactId = (Integer)view.getTag();
         	Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, mCurContactId);
         	Intent viewIntent = new Intent(Intent.ACTION_VIEW, contactUri);
         	Intent editIntent = new Intent(Intent.ACTION_EDIT, contactUri);
         	if (contactUri != null) 
    	        menu.add(0, MENU_ITEM_VIEW_CONTACT, 0, R.string.menu_viewContact).setIntent(viewIntent);
         		menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact).setIntent(editIntent);
         		menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact);
    	 }
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getItemId() == MENU_ITEM_DELETE){
        	Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, mCurContactId);
			getContentResolver().delete(contactUri, null, null);
			mQueryHandler.startQuery(0, null, ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION,
	        		null, null, "sort_key COLLATE LOCALIZED asc");
        }
        return super.onContextItemSelected(item);
    }
    
    class QueryHandler extends AsyncQueryHandler {

        public QueryHandler(Context context) {
            super(context.getContentResolver());
        }       

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {   
        	mAdapter.changeCursor(cursor);
            if (mAdapter.isEmpty()){
            	emptyText.setVisibility(View.VISIBLE);
            }else{
            	emptyText.setVisibility(View.GONE);
            	mAdapter.notifyDataSetChanged();
            }	
            ensureBladeView(); 
        }    
        
        void ensureBladeView() {
            if (mBladeView == null) 
                return;
            if (mAdapter == null || !mAdapter.isEmpty()) {
                mBladeView.setList(getListView());
                getListView().setFastScrollEnabled(false);
                mBladeView.setVisibility(View.VISIBLE);
            } else {
                mBladeView.setVisibility(View.GONE);
                getListView().setFastScrollEnabled(false);
            }
        }
    }
	
}